/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.j002.requirementengineeringmuni.swing;

import cz.muni.fi.j002.requirementengineeringmuni.Game;
import cz.muni.fi.j002.requirementengineeringmuni.Player;
import cz.muni.fi.j002.requirementengineeringmuni.entities.Move;
import cz.muni.fi.j002.requirementengineeringmuni.impl.PlayerImpl;
import cz.muni.fi.j002.requirementengineeringmuni.impl.GameImpl;
import cz.muni.fi.j002.requirementengineeringmuni.util.RuleEngine;
import java.awt.FlowLayout;
import java.math.BigDecimal;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author stefan
 */
public class NewGameJFrame extends javax.swing.JFrame {
    
    private Game game;

    /**
     * Creates new form NewGameJFrame
     */
    public NewGameJFrame(Player firstPlayer, Player secondPlayer) {
        
        this.game = new GameImpl(firstPlayer, secondPlayer);
        
        initComponents();
        
        Move initialMove = new Move(BigDecimal.ZERO, BigDecimal.ZERO, Move.NextCardChoice.HIGHER, Move.NextCardChoice.HIGHER).setCard(this.game.getDeck().drawCard());
        paintCardImages(initialMove);
        this.game.addAndEvaluateNewMove(initialMove);
        
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        firstPlayerNameLabel = new javax.swing.JLabel();
        firstPlayerAmountLabel = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        firstPlayerBidJTextField = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        firstPlayerScoreJLabel = new javax.swing.JLabel();
        firstPlayerAvailableJLabel = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        secondPlayerNameLabel = new javax.swing.JLabel();
        secondPlayerAmountLabel = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        secondPlayerBidJTextField = new javax.swing.JTextField();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        secondPlayerScoreJLabel = new javax.swing.JLabel();
        secondPlayerAvailableJLabel = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        nextCardJButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        cardsJTable = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Player 1");

        firstPlayerNameLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        firstPlayerNameLabel.setText(game.getFirstPlayer().getName());

        firstPlayerAmountLabel.setText(game.getFirstPlayer().getBank().getAccountState().toString());

        jLabel6.setText("score:");

        firstPlayerBidJTextField.setText(this.game.getFirstPlayer().getDefaultBid().toString());

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("lower");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("higher");

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("computer decide");
        jRadioButton3.setEnabled(false);

        firstPlayerScoreJLabel.setText("0");

        firstPlayerAvailableJLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        firstPlayerAvailableJLabel.setText(game.getFirstPlayer().getBank().getAccountState().toString());

        jLabel5.setText("available:");

        jLabel11.setText("bank:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(firstPlayerScoreJLabel))
                            .addComponent(jLabel1)
                            .addComponent(firstPlayerNameLabel)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(firstPlayerAmountLabel)))
                        .addGap(18, 55, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton1)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jRadioButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jRadioButton2)
                                .addComponent(firstPlayerBidJTextField, javax.swing.GroupLayout.Alignment.TRAILING))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(firstPlayerAvailableJLabel)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(firstPlayerBidJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstPlayerNameLabel)
                    .addComponent(jRadioButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton2)
                    .addComponent(firstPlayerAmountLabel)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jRadioButton3)
                    .addComponent(firstPlayerScoreJLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstPlayerAvailableJLabel)
                    .addComponent(jLabel5))
                .addContainerGap())
        );

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Player 2");

        secondPlayerNameLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        secondPlayerNameLabel.setText(game.getSecondPlayer().getName());

        secondPlayerAmountLabel.setText(game.getSecondPlayer().getBank().getAccountState().toString());

        jLabel8.setText("score:");

        secondPlayerBidJTextField.setText(this.game.getSecondPlayer().getDefaultBid().toString());

        buttonGroup2.add(jRadioButton4);
        jRadioButton4.setText("lower");

        buttonGroup2.add(jRadioButton5);
        jRadioButton5.setText("higher");

        buttonGroup2.add(jRadioButton6);
        jRadioButton6.setText("computer decide");
        jRadioButton6.setEnabled(false);

        secondPlayerScoreJLabel.setText("0");

        secondPlayerAvailableJLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        secondPlayerAvailableJLabel.setText(game.getSecondPlayer().getBank().getAccountState().toString());

        jLabel7.setText("available:");

        jLabel10.setText("bank:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(secondPlayerBidJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
                        .addComponent(jLabel2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jRadioButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(secondPlayerNameLabel))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jRadioButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(secondPlayerAmountLabel))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jRadioButton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(secondPlayerScoreJLabel))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(secondPlayerAvailableJLabel)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(secondPlayerBidJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton4)
                    .addComponent(secondPlayerNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton5)
                    .addComponent(secondPlayerAmountLabel)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton6)
                    .addComponent(jLabel8)
                    .addComponent(secondPlayerScoreJLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(secondPlayerAvailableJLabel)
                    .addComponent(jLabel7))
                .addContainerGap())
        );

        nextCardJButton.setText("Next card");
        nextCardJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextCardJButtonActionPerformed(evt);
            }
        });

        jButton2.setText("Save game");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        cardsJTable.setModel(new CardsJTableModel().setGame(this.game));
        jScrollPane1.setViewportView(cardsJTable);
        cardsJTable.getColumnModel().getColumn(0).setResizable(false);
        cardsJTable.getColumnModel().getColumn(1).setResizable(false);
        cardsJTable.getColumnModel().getColumn(2).setResizable(false);
        cardsJTable.getColumnModel().getColumn(3).setResizable(false);

        jButton3.setText("End game");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 145, Short.MAX_VALUE)
        );

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("new card");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("previous card");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(111, 111, 111)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(0, 14, Short.MAX_VALUE))
                                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jButton2)
                                .addGap(18, 18, 18)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(nextCardJButton)
                        .addGap(60, 60, 60))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(nextCardJButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton3)
                            .addComponent(jButton2)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel9.setText("bid");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(53, 53, 53)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel9)
                        .addGap(0, 103, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.dispose();
        SplashJFrame.getInstance().setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    
    private void nextCardJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextCardJButtonActionPerformed
        
        if(!bidsOk() || !predictionsOk()) {
            return; // one or both of the players did not bid correctly / did not set predictions for the next round
        } 
        
        if(game.getDeck().hasCardsLeft()) {
            Move move = loadMove();
            move.setCard(game.getDeck().drawCard());
            
            boolean resetFields = game.getLastPlayedMove() == null ? true : !RuleEngine.repeatMove(game.getLastPlayedMove(), move);
                    
            paintCardImages(move);
            
            game.addAndEvaluateNewMove(move);
            
            ((CardsJTableModel) cardsJTable.getModel()).fireTableDataChanged();
            
            firstPlayerScoreJLabel.setText(game.getFirstPlayerScore().toString());
            firstPlayerAvailableJLabel.setText(game.getFirstPlayer().getBank().getAccountState().add(game.getFirstPlayerScore()).toString());
            
            secondPlayerScoreJLabel.setText(game.getSecondPlayerScore().toString());
            secondPlayerAvailableJLabel.setText(game.getSecondPlayer().getBank().getAccountState().add(game.getSecondPlayerScore()).toString());
            
            if(resetFields) {
                resetFields();
            }
        } else {
            throw new IllegalStateException("No more cards in the deck to be drawn");
        }
        
        if(!game.getDeck().hasCardsLeft()) {
            nextCardJButton.setEnabled(false);       
            game.finishGame();
            if(null != game.getWinner()) {
                JOptionPane.showMessageDialog(null, "Player " + game.getWinner().getName() + " wins!");
            } else {
                JOptionPane.showMessageDialog(null, "We had a tie match!");
            }
        }
    }//GEN-LAST:event_nextCardJButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JOptionPane.showMessageDialog(null, "Not implemented yet");
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JTable cardsJTable;
    private javax.swing.JLabel firstPlayerAmountLabel;
    private javax.swing.JLabel firstPlayerAvailableJLabel;
    private javax.swing.JTextField firstPlayerBidJTextField;
    private javax.swing.JLabel firstPlayerNameLabel;
    private javax.swing.JLabel firstPlayerScoreJLabel;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton nextCardJButton;
    private javax.swing.JLabel secondPlayerAmountLabel;
    private javax.swing.JLabel secondPlayerAvailableJLabel;
    private javax.swing.JTextField secondPlayerBidJTextField;
    private javax.swing.JLabel secondPlayerNameLabel;
    private javax.swing.JLabel secondPlayerScoreJLabel;
    // End of variables declaration//GEN-END:variables

    
    private void resetFields() {
                firstPlayerBidJTextField.setText(game.getFirstPlayer().getDefaultBid().toString());
                buttonGroup1.clearSelection();
                secondPlayerBidJTextField.setText(game.getSecondPlayer().getDefaultBid().toString());
                buttonGroup2.clearSelection();
    }
    
    private boolean bidsOk() {
        if(!game.canFirstPlayerMakeABet(new BigDecimal(firstPlayerBidJTextField.getText()))) {
            JOptionPane.showMessageDialog(null, "First player bid more than he disposes with");
            return false;
        }
        if(!game.canSecondPlayerMakeABet(new BigDecimal(secondPlayerBidJTextField.getText()))) {
            JOptionPane.showMessageDialog(null, "Second player bid more than he disposes with");
            return false;
        }
        
        return true;
    }

    private boolean predictionsOk() {
        if(!jRadioButton1.isSelected() && !jRadioButton2.isSelected() && !jRadioButton3.isSelected()) {
            JOptionPane.showMessageDialog(null, "First player did not choose a prediction for the next round");
            return false;
        }
        if(!jRadioButton4.isSelected() && !jRadioButton5.isSelected() && !jRadioButton6.isSelected()) {
            JOptionPane.showMessageDialog(null, "Second player did not choose a prediction for the next round");
            return false;
        }
        
        return true;
    }

    private Move loadMove() {
        return new Move(new BigDecimal(firstPlayerBidJTextField.getText()), 
                        new BigDecimal(secondPlayerBidJTextField.getText()), 
                        jRadioButton1.isSelected() ? Move.NextCardChoice.LOWER : Move.NextCardChoice.HIGHER, 
                        jRadioButton4.isSelected() ? Move.NextCardChoice.LOWER : Move.NextCardChoice.HIGHER);
    }

    private void paintCardImages(Move move) {
        jPanel4.removeAll();
        FlowLayout layout = new FlowLayout();
        layout.setAlignment(FlowLayout.LEFT);
        jPanel4.setLayout(layout);
        
        Move lastMove = game.getLastPlayedMove(); // can be null
        if(null != lastMove) {
            JLabel previousPicLabel = new JLabel(new ImageIcon(lastMove.getCard().getImage()));
            jPanel4.add(previousPicLabel);
            previousPicLabel.setVisible(true);
        }

        JLabel picLabel = new JLabel(new ImageIcon(move.getCard().getImage()));
        jPanel4.add(picLabel);
        picLabel.setVisible(true);

        jPanel4.setVisible(true);
        jPanel4.validate();
    }
}
